key_left    =   keyboard_check(ord("Z"));
key_right   =   keyboard_check(ord("X"));
key_jump    =   keyboard_check_pressed(vk_space);


// store last status
laststatus = status

// move based on the key input
if (key_left) 
{
    if (laststatus!=JUMPING1 && laststatus!=JUMPING2 && status !=FALLING)
    {
        status=RUNNING
    }
    dir=LEFT
    scr_playermove(-self.walkspeed,0,false)
}
if (key_right)
{
    if (laststatus!=JUMPING1 && laststatus!=JUMPING2  && status !=FALLING)
    {
        status=RUNNING
    }
    dir=RIGHT
    scr_playermove(self.walkspeed,0,false)
}
if (key_jump)
{
    if (jumpcount == 0) 
    {
          status=JUMPING1
          self.dy=0
          scr_playermove(0,self.jumpstrength,false)
          jumpcount=1
          audio_play_sound(snd_jump_1,0,false)
//          landed=false
    }
    else
    {
        if (jumpcount == 1)
        {
            jumpcount = 2
            self.dy = 0
            scr_playermove(0,self.jumpstrength,false)
            status = JUMPING2
            audio_play_sound(snd_jump_2,0,false)
        }   
    }
    
}
if ((!key_jump) && (!key_left) && (!key_right)) && (status != JUMPING1 && status != JUMPING2) 
{
    if isgrounded
    {
        status=IDLE
    }
    else
    {
        status=FALLING
    }
    if (abs(self.dx) < self.walkspeed+1)
    {
        self.dx=0;
    }
    else
    {
        if self.dx>0
        {
            scr_playermove(-self.walkspeed,0,false);
        }
        else
        {
            scr_playermove(self.walkspeed,0,false);
        }
    } 
}

// apply gravity
scr_playermove(0,self.fallspeed,false)

// check to see if we have landed on a special tile first like bouncers and if so move!
var hitinstance = instance_place(x,y+(self.dy/scale),par_bouncer)
if hitinstance != noone
{
    switch (hitinstance.collision_type)
    {
        case BOUNCE_UP:
            scr_playermove(0,-10000,true)
            break;
        case BOUNCE_LEFT:
            scr_playermove(-10000,-10000,true) 
            break;
        case BOUNCE_RIGHT:
            scr_playermove(10000,-10000,true) 
            break;
    }
    status=JUMPING1
}

// true and move taking into consideration any collision.  If we hit a collision move to a point just before the collision

// check horizontal
if place_meeting(x+(self.dx/scale),y,par_solid)
{
    var counter=0
    while(!place_meeting(round(x+sign(dx)),y,par_solid) && counter < 1000) //added counter just incase 
    {
        x = x + sign(dx)
        counter=counter+1
    }
    self.dx = 0
}

//update xpos and x position
self.xpos = self.xpos + self.dx   
self.x = round(self.xpos / scale)
self.xpos = x * scale

// check vertical
if place_meeting(x,y+(self.dy/scale),par_solid)
{    
    var counter=0
    // sorts out our collision regardless if its above us etc.
    while(!place_meeting(x,round(y+sign(dy)),par_solid) && counter < 1000) //added counter just incase 
    {
        y = y + sign(dy)
        counter=counter+1
    }
    // check if we are moving downwards
    if dy>0 
    {
        isgrounded = true
        jumpcount=0
        if (landed == false)
        {
            if (status!=RUNNING && status!=IDLE)
            {
                audio_play_sound(snd_player_land,0,false)
            }
            landed=true
        }
    }
    self.dy = 0
    if (status != RUNNING) 
    {
        status=IDLE
    }
}
else
{
    isgrounded = false
    if (status!=JUMPING1 && status!=JUMPING2)
    {
        if (!place_meeting(x,y+1,par_solid))
        {
            status=FALLING
            landed=false
        }
    }
}

// update ypos and y position
self.ypos = self.ypos + self.dy
self.y = round(self.ypos/scale)
self.ypos = y * scale



// select the correct sprite image if we are doing something different than the last frame
if (self.status != self.laststatus)
{
    switch (self.status)
    { 
      case IDLE:
           sprite_index=spr_player_idle;
           break;
      case RUNNING:
           sprite_index=spr_player_run;
           image_speed=animspeed;
           break;
      case FALLING:
           sprite_index=spr_player_fall;
           break;
      case JUMPING1:
           sprite_index=spr_player_jump;
           break
      case JUMPING2:
           sprite_index=spr_player_spin;
           image_speed=animspeed;
           break;
    }     
}

// face the sprite image in the right direction
if dir=RIGHT 
{
    image_xscale = 1
}
else
{
    image_xscale = -1
}

// center camera view
scr_gamecenterview(self.x, self.y)



        
